﻿package com.eve.microworld.main;

import java.util.ArrayList;
import java.util.Arrays;

import android.app.ListActivity;
import android.content.Context;
import android.graphics.Color;
import android.os.Bundle;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.eve.microworld.R;
import com.eve.microworld.common.view.DraggableListView;

/**
 * 频道管理界面
 * @author Norsie
 * @create 12-11-29
 */
public class ChannelManagerActivity extends ListActivity{
    private Context mContext=ChannelManagerActivity.this;
    private static String[] items = { "浙江", "江苏", "广东", "上海","未定阅", "北京", "新疆","天津","山东","辽宁","甘肃","黑龙江" };
    private ChannelManagerAdapter adapter = null;
    private ArrayList<String> array = new ArrayList<String>( Arrays.asList(items));
    private ArrayList<String> afterArray=new ArrayList<String>();

    @Override
    public void onCreate(Bundle icicle) {
        super.onCreate(icicle);
        setContentView(R.layout.activity_channel_main);

        adapter = new ChannelManagerAdapter();
        setListAdapter(adapter);

        DraggableListView tlv = (DraggableListView) getListView();

        tlv.setDropListener(onDrop);
        tlv.setRemoveListener(onRemove);
        tlv.getAdapter();
    }

    private DraggableListView.DropListener onDrop = new DraggableListView.DropListener() {
        @Override
        public void drop(int from, int to) {
            String item = adapter.getItem(from);

            adapter.remove(item);
            adapter.insert(item, to);
            Toast.makeText(mContext, adapter.getList().toString(),
                    Toast.LENGTH_SHORT).show();
            afterArray=adapter.getList();  //保存变化后的排序
        }
    };

    private DraggableListView.RemoveListener onRemove = new DraggableListView.RemoveListener() {
        @Override
        public void remove(int which) {
            adapter.remove(adapter.getItem(which));
        }
    };

    class ChannelManagerAdapter extends ArrayAdapter<String> {
        ChannelManagerAdapter() {
            super(mContext, R.layout.adapter_channelmanager_row, array);
        }
        public ArrayList<String> getList() {
            return array;
        }

        public View getView(int position, View convertView, ViewGroup parent) {
            View row = convertView;
            if (row == null) {
                LayoutInflater inflater = getLayoutInflater();
                row = inflater.inflate(R.layout.adapter_channelmanager_row, parent, false);
            }
            row.findViewById(R.id.adapter_channelmanager_icon).setVisibility(View.VISIBLE);
            ((TextView)row.findViewById(R.id.adapter_channelmanager_label)).setGravity(Gravity.LEFT);
            ((TextView)row.findViewById(R.id.adapter_channelmanager_label)).setBackgroundColor(Color.BLACK);
            ((TextView)row.findViewById(R.id.adapter_channelmanager_label)).setLayoutParams(new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.WRAP_CONTENT));
            if(array.get(position).equals("已订阅")||array.get(position).equals("未定阅")) {
                row.findViewById(R.id.adapter_channelmanager_icon).setVisibility(View.GONE);
                ((TextView)row.findViewById(R.id.adapter_channelmanager_label)).setBackgroundColor(Color.parseColor("#eeeeee"));
                ((TextView)row.findViewById(R.id.adapter_channelmanager_label)).setGravity(Gravity.CENTER);
                ((TextView)row.findViewById(R.id.adapter_channelmanager_label)).setLayoutParams(new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.FILL_PARENT, RelativeLayout.LayoutParams.WRAP_CONTENT));
            }
            TextView label = (TextView) row.findViewById(R.id.adapter_channelmanager_label);
            label.setText(array.get(position));
            return (row);
        }
    }
}
